package cn.baiweigang.uu.api.linearswap;

import cn.baiweigang.uu.api.BaseApi;
import cn.baiweigang.uu.enums.ContractCodeEnum;
import cn.baiweigang.uu.enums.PeriodEnum;
import cn.baiweigang.uu.model.KLineInfo;
import com.huobi.linearswap.api.request.account.SwapMarketHistoryKlineRequest;
import com.huobi.linearswap.api.response.market.SwapMarketHistoryKlineResponse;
import com.huobi.linearswap.api.service.market.MarketAPIServiceImpl;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class MarketLinearSwapApi extends BaseApi {
    MarketAPIServiceImpl huobiAPIService = new MarketAPIServiceImpl();
    public List<KLineInfo> getHistoryData(ContractCodeEnum contractCode, PeriodEnum periodEnum, int size){
        List<KLineInfo> kLineInfoList = new ArrayList<>();
        SwapMarketHistoryKlineRequest request = SwapMarketHistoryKlineRequest.builder()
                .contractCode(contractCode.getCode())
                .period(periodEnum.getCode())
                .size(size)
                .build();
        SwapMarketHistoryKlineResponse response = huobiAPIService.getSwapMarketHistoryKline(request);
        if (response!=null && response.getStatus().equalsIgnoreCase("ok")){
            List<SwapMarketHistoryKlineResponse.DataBean> dataBeanList=response.getData();
            for (SwapMarketHistoryKlineResponse.DataBean dataBean:dataBeanList){
                KLineInfo kLineInfo=convert2KLineInfo(dataBean);
                kLineInfoList.add(kLineInfo);
            }
        }
        return kLineInfoList;
    }

    private KLineInfo convert2KLineInfo(SwapMarketHistoryKlineResponse.DataBean dataBean) {
        KLineInfo kLineInfo=new KLineInfo();
        kLineInfo.setAmount(dataBean.getAmount());
        kLineInfo.setClose(dataBean.getClose());
        kLineInfo.setCount(dataBean.getCount());
        kLineInfo.setHigh(dataBean.getHigh());
        kLineInfo.setId(dataBean.getId());
        kLineInfo.setLow(dataBean.getLow());
        kLineInfo.setOpen(dataBean.getOpen());
        kLineInfo.setTradeTurnover(dataBean.getTradeTurnover());
        kLineInfo.setVol(dataBean.getVol());
        return kLineInfo;
    }
}
